﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.models {
    import flash.events.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class EnergyModel extends Actor {

        public var tickModel:TickModel;
        public var sessionModel:SessionModel;
        private var _energyStartedAt:int;
        private var _energyUsed:int;
        private var _energyMax:int;
        private var _energyRefillDuration:int;
        private var _boostDuration:int;
        private var _differenceTimer:Timer;
        private var _refillTimer:Timer;
        private var _energyExtra:int;
        private var _pendingCallsToBackendCount:int;// = 0

        public function get energy():int{
            var _local1:int = ((this.energyMax - this.energyUsed) + this.energyRefilled);
            _local1 = Math.min(_local1, this.energyMax);
            _local1 = Math.max(_local1, 0);
            return ((_local1 - this.pendingCallsToBackendCount));
        }
        public function refill(_arg1:int):void{
            this.energyExtra = (this.energyExtra + Math.max(0, (_arg1 - (this.energyMax - this.energy))));
            this.energyUsed = Math.max(0, (this.energyUsed - _arg1));
        }
        public function get energyStartedAt():int{
            return (this._energyStartedAt);
        }
        public function set energyStartedAt(_arg1:int):void{
            this._energyStartedAt = _arg1;
        }
        public function get energyUsed():int{
            return (this._energyUsed);
        }
        public function set energyUsed(_arg1:int):void{
            this._energyUsed = _arg1;
            dispatch(new UserModelEvent(UserModelEvent.ON_MODEL_CHANGED));
        }
        public function get energyMax():int{
            return (this._energyMax);
        }
        public function set energyMax(_arg1:int):void{
            var _local2:Boolean = ((!((this._energyMax == 0))) && (!((this._energyMax == _arg1))));
            this._energyMax = _arg1;
            if (_local2){
                dispatch(new Event(EventConstants.SHOW_EXTRA_ENERGY_POPUP));
            };
        }
        public function get energyRefillDuration():int{
            return (this._energyRefillDuration);
        }
        public function set energyRefillDuration(_arg1:int):void{
            this._energyRefillDuration = _arg1;
        }
        public function get nextRefillIn():int{
            var _local1:int = ((this.energyStartedAt + (this.energyRefillDuration * this.energyUsed)) - this.timestamp);
            _local1 = (_local1 % this.energyRefillDuration);
            return (_local1);
        }
        public function get boostDuration():int{
            return (this._boostDuration);
        }
        public function set boostDuration(_arg1:int):void{
            this._boostDuration = _arg1;
        }
        public function get pendingCallsToBackendCount():int{
            return (this._pendingCallsToBackendCount);
        }
        public function set pendingCallsToBackendCount(_arg1:int):void{
            this._pendingCallsToBackendCount = _arg1;
        }
        private function get energyRefilled():int{
            var _local1:int = ((this.timestamp - this.energyStartedAt) / this.energyRefillDuration);
            _local1 = Math.max(0, _local1);
            return (_local1);
        }
        public function get energyExtra():int{
            return (this._energyExtra);
        }
        public function set energyExtra(_arg1:int):void{
            this._energyExtra = _arg1;
        }
        private function get timestamp():int{
            var _local1:int;
            if (((this.tickModel) || (this.sessionModel))){
                _local1 = ((this.tickModel.timestamp) || (this.sessionModel.timestamp));
            } else {
                _local1 = new Date().time;
            };
            return (_local1);
        }
        public function startDifferenceCounter():void{
            this.stopDifferenceCounter();
            this.stopRefillCounter();
            this._differenceTimer = new Timer(1000);
            this._differenceTimer.addEventListener(TimerEvent.TIMER, this.onDifferenceTick);
            this._differenceTimer.start();
        }
        private function onDifferenceTick(_arg1:TimerEvent):void{
            if (this.nextRefillIn <= 0){
                this.stopDifferenceCounter();
                this.startRefillCounter();
                dispatch(new UserModelEvent(UserModelEvent.ON_MODEL_CHANGED));
            };
        }
        private function stopDifferenceCounter():void{
            if (((this._differenceTimer) && (this._differenceTimer.running))){
                this._differenceTimer.stop();
            };
        }
        private function startRefillCounter():void{
            this.stopRefillCounter();
            this._refillTimer = new Timer((this.energyRefillDuration * 1000));
            this._refillTimer.addEventListener(TimerEvent.TIMER, this.onRefillTick);
            this._refillTimer.start();
        }
        private function onRefillTick(_arg1:TimerEvent):void{
            dispatch(new UserModelEvent(UserModelEvent.ON_MODEL_CHANGED));
            if (this.energy == this.energyMax){
                this.stopRefillCounter();
            };
        }
        private function stopRefillCounter():void{
            if (((this._refillTimer) && (this._refillTimer.running))){
                this._refillTimer.stop();
            };
        }

    }
}//package net.wooga.pets.models 
